<template>
  <view class="app-container">
  </view>
</template>

<script setup>
import { onLaunch, onShow, onHide, onError } from '@dcloudio/uni-app';
import { useThemeStore } from './stores/theme';
import { closeAllWebSocketConnections, getWebSocketStats, debugWebSocketConnections, cleanupInvalidConnections } from '@/utils/websocket-manager';

// 应用启动时
onLaunch(() => {
    console.log('App Launch');
    // 初始化主题
    const themeStore = useThemeStore();
    themeStore.loadFromStorage();
    
    // 清理无效连接
    cleanupInvalidConnections();
    
    // 输出WebSocket连接调试信息
    debugWebSocketConnections();
});

// 应用显示时
onShow(() => {
  console.log('App Show');
  // 清理无效连接
  cleanupInvalidConnections();
  
  // 输出WebSocket连接调试信息
  debugWebSocketConnections();
});

// 应用隐藏时
onHide(() => {
  console.log('App Hide');
  // 应用隐藏时，可以选择是否关闭WebSocket连接
  // 这里暂时不关闭，保持连接以便快速恢复
  console.log('WebSocket连接统计:', getWebSocketStats());
});

// 全局错误捕获
onError((error) => {
  console.error('App Error:', error);
  
  // 如果是WebSocket相关错误，尝试关闭所有连接
  if (error && (error.message.includes('WebSocket') || error.message.includes('websocket'))) {
    console.log('检测到WebSocket错误，关闭所有连接');
    closeAllWebSocketConnections();
  }
  
  // 输出调试信息
  debugWebSocketConnections();
  
  uni.showToast({
    title: '程序出现错误，请稍后重试',
    icon: 'none'
  });
});


</script>

<style lang="scss">
@use "./src/styles/variables.scss" as *;
@import "./static/styles/iconfont.css";

.app-container {
  width: 100%;
  min-height: 100vh;
}

/* PC端适配 */
@media (min-width: 1024px) {
  .app-container {
    max-width: 1200px;
    margin: 0 auto;
    padding: 0 20px;
  }
}

/* 全局样式 */
page {
  font-family: -apple-system, BlinkMacSystemFont, 'Helvetica Neue', Helvetica,
    Segoe UI, Arial, Roboto, 'PingFang SC', 'miui', 'Hiragino Sans GB', 'Microsoft Yahei',
    sans-serif;
  background-color: var(--bg-color);
  color: var(--text-color);
  transition: background-color $transition-normal, color $transition-normal;
}

/* 去除按钮默认样式 */
button {
  background: none;
  padding: 0;
  margin: 0;
  line-height: 1;
  border: none;
  outline: none;
  box-sizing: border-box;
  text-align: left;
  
  &::after {
    border: none;
  }
}

/* 去除滚动条 */
::-webkit-scrollbar {
  display: none;
  width: 0;
  height: 0;
  color: transparent;
}

/* 全局动画 */
.fade-enter-active, .fade-leave-active {
  transition: opacity $transition-normal;
}
.fade-enter-from, .fade-leave-to {
  opacity: 0;
}
</style>
